<?php
class Order_Manager
{
	private $db_id;
	private $path;
	private $is_editing;
	private $editing_path;
	private $status;
	
	private $info;
	
	private $is_load_xml;
	
	public static $STATUS_CREATING = 'creating';
	public static $STATUS_SAVED = 'saved';
	
	public static function getOrderManager($id)
	{
		$dt = new DataTable(constant('DB_TABLE_ORDER_MANAGER'));
		$orderManagerData = $dt->get(intval($id));
		if($orderManagerData == null)
			return null;
		$orderManager = new Order_Manager($orderManagerData['id'],$orderManagerData['path'],$orderManagerData['status'],$orderManagerData['is_editing'],$orderManagerData['editing_path']);

		return $orderManager;
	}
	public static function createOrderManager()
	{
		$dt = new DataTable(constant('DB_TABLE_ORDER_MANAGER'));
		$orderDefXmlPath = constant('PATH_ORDER_EDITING_DIR').'/order_'.time().Util::makeRandomStr(5).'.xml';
		$templatePath = constant('SITE_WEB_DISK_DIR').'/config/order_template.xml';
		copy($templatePath,constant('SITE_WEB_DISK_DIR').'/'.$orderDefXmlPath);
		
		$orderManagerData = array('status'=>Order_Manager::$STATUS_CREATING,'path'=>$orderDefXmlPath,'status'=>'creating','is_editing'=>'','editing_path'=>'');
		
		$dt->save($orderManagerData);
		$orderManager = new Order_Manager($orderManagerData['id'],$orderManagerData['path'],$orderManagerData['status'],$orderManagerData['is_editing'],$orderManagerData['editing_path']);

		return $orderManager;
		
	}
	public function Order_Manager($db_id,$path,$status,$is_editing,$editing_path)
	{
		$this->db_id = $db_id;
		$this->path = $path;
		$this->status = $status;
		$this->is_editing = $is_editing == 'Y';
		$this->editing_path = $editing_path;
		$this->info = null;
		$this->is_load_xml = false;
		
	}
	private function reload()
	{
		$dt = new DataTable(constant('DB_TABLE_ORDER_MANAGER'));
		$orderManagerData = $dt->get(intval($this->db_id));
		$this->db_id = $orderManagerData['id'];
		$this->path = $orderManagerData['path'];
		$this->status = $orderManagerData['status'];
		$this->is_editing = $orderManagerData['is_editing'] == 'Y';
		$this->editing_path = $orderManagerData['editing_path'];
	}
	public function init()
	{
		$this->_loadXmlData();
	}
	public function setEditing()
	{
		if($this->is_editing)
			return;
		$this->is_editing = true;
		$editing_path = constant('PATH_ORDER_EDITING_DIR').'/order_'.time().Util::makeRandomStr(5).'.xml';
		copy($this->path,constant('SITE_WEB_DISK_DIR').'/'.$editing_path);
		$updateData = array('id'=>$this->db_id,'is_editing'=>'Y','editing_path'=>$editing_path);
		$dt = new DataTable(constant('DB_TABLE_ORDER_MANAGER'));
		$dt->save($updateData);
		$this->editing_path = $editing_path;
		$this->init();
	}
	public function setOrderCreateDate($date)
	{
		$refOrderInfo = &$this->info['order'];
		$refOrderInfo['created'] = $date;
	}
	public function setNotEditing()
	{
		if(!$this->is_editing)
			return;
		$this->is_editing = false;
		copy($this->editing_path,$this->path);
		unlink($this->editing_path);
		$updateData = array('id'=>$this->db_id,'is_editing'=>'N','editing_path'=>'');
		$dt = new DataTable(constant('DB_TABLE_ORDER_MANAGER'));
		$dt->save($updateData);
		$this->editing_path = '';
	}
	public function persistOrderInfo()
	{
		file_put_contents($this->getWoringXmlPath(),XML::XML_serialize($this->info));
		$this->reIndex();
	}
	public function setCustomerInfo($customerData,$onlyUpdate = true)
	{
		if(!$this->is_load_xml)
			$this->_loadXmlData();
			
		$customer = &$this->info['order']['customerinfo'];
		//name
		if($onlyUpdate)
		{
			if(isset($customerData['name']))
			{
				$customer['name'] = $customerData['name'];
			}
		}else{
			$customer['name'] = $customerData['name'];
		}
		//birthday
		if($onlyUpdate)
		{
			if(isset($customerData['birthday_year']))
			{
				$customer['birthday']['year'] = $customerData['birthday_year'];
			}
		}else{
			$customer['birthday']['year'] = $customerData['birthday_year'];
		}
		if($onlyUpdate)
		{
			if(isset($customerData['birthday_month']))
			{
				$customer['birthday']['month'] = $customerData['birthday_month'];
			}
		}else{
			$customer['birthday']['month'] = $customerData['birthday_month'];
		}
		if($onlyUpdate)
		{
			if(isset($customerData['birthday_date']))
			{
				$customer['birthday']['date'] = $customerData['birthday_date'];
			}
		}else{
			$customer['birthday']['date'] = $customerData['birthday_date'];
		}
	
		//telephone
		if($onlyUpdate)
		{
			if(isset($customerData['tel_part_one']))
			{
				$customer['tel']['part_one'] = $customerData['tel_part_one'];
			}
		}else{
			$customer['tel']['part_one'] = $customerData['tel_part_one'];
		}
		if($onlyUpdate)
		{
			if(isset($customerData['tel_part_two']))
			{
				$customer['tel']['part_two'] = $customerData['tel_part_two'];
			}
		}else{
			$customer['tel']['part_two'] = $customerData['tel_part_two'];
		}

		//mobile
		if($onlyUpdate)
		{
			if(isset($customerData['mobile']))
			{
				$customer['mobile'] = $customerData['mobile'];
			}
		}else{
			$customer['mobile'] = $customerData['mobile'];
		}
		//address
		if($onlyUpdate)
		{
			if(isset($customerData['address_part_one']))
			{
				$customer['address']['part_one'] = $customerData['address_part_one'];
			}
		}else{
			$customer['address']['part_one'] = $customerData['address_part_one'];
		}
		if($onlyUpdate)
		{
			if(isset($customerData['address_part_two']))
			{
				$customer['address']['part_two'] = $customerData['address_part_two'];
			}
		}else{
			$customer['address']['part_two'] = $customerData['address_part_two'];
		}
		if($onlyUpdate)
		{
			if(isset($customerData['address_part_three']))
			{
				$customer['address']['part_three'] = $customerData['address_part_three'];
			}
		}else{
			$customer['address']['part_three'] = $customerData['address_part_three'];
		}
	
		//email
		if($onlyUpdate)
		{
			if(isset($customerData['email']))
			{
				$customer['email'] = $customerData['email'];
			}
		}else{
			$customer['email'] = $customerData['email'];
		}
		
		if($onlyUpdate)
		{
			if(isset($customerData['email2']))
			{
				$customer['email2'] = $customerData['email2'];
			}
		}else{
			$customer['email2'] = $customerData['email2'];
		}
	}
	public function getCustomerInfo()
	{
		if(!$this->is_load_xml)
			$this->_loadXmlData();
			
		return $this->info['order']['customerinfo'];
	}
	public function setMainInfo($setinfo,$onlyUpdate = true, $withEstimate = true)
	{
		if(!$this->is_load_xml)
			$this->_loadXmlData();
			
		$mainInfo = &$this->info['order'];
		//name
		if($onlyUpdate)
		{
			if(isset($setinfo['department']))
			{
				$mainInfo['department'] = $setinfo['department'];
			}
		}else{
			$mainInfo['department'] = $setinfo['department'];
		}
		//number
		if($onlyUpdate)
		{
			if(isset($setinfo['number']))
			{
				$mainInfo['number'] = $setinfo['number'];
			}
		}else{
			$mainInfo['number'] = $setinfo['number'];
		}
		//department_worker
		if($onlyUpdate)
		{
			if(isset($setinfo['department_worker']))
			{
				$mainInfo['department_worker'] = $setinfo['department_worker'];
			}
		}else{
			$mainInfo['department_worker'] = $setinfo['department_worker'];
		}
		if ( $withEstimate) {
		$this->reEstimate();
		}
		
	}
	public function getMainInfo()
	{
		if(!$this->is_load_xml)
			$this->_loadXmlData();
		return $this->info['order'];
	}
	public function deleteZoomlistById($idList)
	{
		if(!$this->is_load_xml)
			$this->_loadXmlData();
		$info = &$this->info['order'];
		$newZoomList = array();
		$newFrameList = array();
		foreach($info['zoom_list'] as $tmpData)
		{
			if(!in_array($tmpData['size_id'],$idList))
			{
				$newZoomList[] =  $tmpData;
			}
		}
		
		foreach($info['zoom_frame_list'] as $tmpData)
		{
			if(!in_array($tmpData['size_id'],$idList))
			{
				$newFrameList[] =  $tmpData;
			}
	   }
		$info['zoom_list'] = $newZoomList;
		$info['zoom_frame_list'] = $newFrameList;
		$this->reEstimate();
	}
	public function setInfo($setinfo,$onlyUpdate = true)
	{
		if(!$this->is_load_xml)
			$this->_loadXmlData();
			
		$info = &$this->info['order'];
		//catalog
		if($onlyUpdate)
		{
			if(isset($setinfo['catalog']))
			{
				$info['catalog'] = $setinfo['catalog'];
			}
		}else{
			$info['catalog'] = $setinfo['catalog'];
		}
		//type
		if($onlyUpdate)
		{
			if(isset($setinfo['type']))
			{
				$info['type'] = $setinfo['type'];
			}
		}else{
			$info['type'] = $setinfo['type'];
		}
		//disk_number
		if($onlyUpdate)
		{
			if(isset($setinfo['disk_number']))
			{
				$info['disk_number'] = $setinfo['disk_number'];
			}
		}else{
			$info['disk_number'] = $setinfo['disk_number'];
		}
		//disk number add
		if($onlyUpdate)
		{
			if(isset($setinfo['disk_number_add']))
			{
				$info['disk_number_add'] = $setinfo['disk_number_add'];
			}
		}else{
			$info['disk_number_add'] = $setinfo['disk_number_add'];
		}
		//benzu number
		if($onlyUpdate)
		{
			if(isset($setinfo['benzu_number']))
			{
				$info['benzu_number'] = $setinfo['benzu_number'];
			}
		}else{
			$info['benzu_number'] = $setinfo['benzu_number'];
		}
		//benzu style
		if(isset($setinfo['benzu_style']) && is_array($setinfo['benzu_style']))
			$setinfo['benzu_style'] = implode(',',$setinfo['benzu_style']);
		if($onlyUpdate)
		{
			if(isset($setinfo['benzu_style']))
			{
				$info['benzu_style'] = $setinfo['benzu_style'];
			}
		}else{
			$info['benzu_style'] = $setinfo['benzu_style'];
		}
		//person number
		if($onlyUpdate)
		{
			if(isset($setinfo['person_number']))
			{
				$info['person_number'] = $setinfo['person_number'];
			}
		}else{
			$info['person_number'] = $setinfo['person_number'];
		}
		if($onlyUpdate)
		{
			if(isset($setinfo['person_number2']))
			{
				$info['person_number2'] = $setinfo['person_number2'];
			}
		}else{
			$info['person_number2'] = $setinfo['person_number2'];
		}
		//person number add
		if($onlyUpdate)
		{
			if(isset($setinfo['person_number_add']))
			{
				$info['person_number_add'] = $setinfo['person_number_add'];
			}
		}else{
			$info['person_number_add'] = $setinfo['person_number_add'];
		}
		if($onlyUpdate)
		{
			if(isset($setinfo['person_number2_add']))
			{
				$info['person_number2_add'] = $setinfo['person_number2_add'];
			}
		}else{
			$info['person_number2_add'] = $setinfo['person_number2_add'];
		}
		//dress number
		if($onlyUpdate)
		{
			if(isset($setinfo['dress_number']))
			{
				$info['dress_number'] = $setinfo['dress_number'];
			}
		}else{
			$info['dress_number'] = $setinfo['dress_number'];
		}
		if($onlyUpdate)
		{
			if(isset($setinfo['dress_number2']))
			{
				$info['dress_number2'] = $setinfo['dress_number2'];
			}
		}else{
			$info['dress_number2'] = $setinfo['dress_number2'];
		}
		//dress number add
		if($onlyUpdate)
		{
			if(isset($setinfo['dress_number_add']))
			{
				$info['dress_number_add'] = $setinfo['dress_number_add'];
			}
		}else{
			$info['dress_number_add'] = $setinfo['dress_number_add'];
		}
		if($onlyUpdate)
		{
			if(isset($setinfo['dress_number2_add']))
			{
				$info['dress_number2_add'] = $setinfo['dress_number2_add'];
			}
		}else{
			$info['dress_number2_add'] = $setinfo['dress_number2_add'];
		}
		//zoom size
		if($onlyUpdate)
		{
			if(isset($setinfo['zoom_list']))
			{
				$info['zoom_list'] = $setinfo['zoom_list'];
			}
		}else{
			$info['zoom_list'] = $setinfo['zoom_list'];
		}
		//zoom size gift
		if($onlyUpdate)
		{
			if(isset($setinfo['zoom_list_isgift']))
			{
				$info['zoom_list_isgift'] = $setinfo['zoom_list_isgift'] && $setinfo['zoom_list_isgift'] == 'Y'?'Y':'N';
			}
		}else{
			$info['zoom_list_isgift'] = $setinfo['zoom_list_isgift'] && $setinfo['zoom_list_isgift'] == 'Y'?'Y':'N';
		}
		//zoom frame list
		if($onlyUpdate)
		{
			if(isset($setinfo['zoom_frame_list']))
			{
				$info['zoom_frame_list'] = $setinfo['zoom_frame_list'];
			}
		}else{
			$info['zoom_frame_list'] = $setinfo['zoom_frame_list'];
		}
		//zoom frame gift
		if($onlyUpdate)
		{
			if(isset($setinfo['zoom_frame_list_isgift']))
			{
				$info['zoom_frame_list_isgift'] = $setinfo['zoom_frame_list_isgift'] && $setinfo['zoom_frame_list_isgift'] == 'Y'?'Y':'N';
			}
		}else{
			$info['zoom_frame_list_isgift'] = $setinfo['zoom_frame_list_isgift'] && $setinfo['zoom_frame_list_isgift'] == 'Y'?'Y':'N';
		}
		//gift list
		if($onlyUpdate)
		{
			if(isset($setinfo['gift_list']))
			{
				$info['gift_list'] = $setinfo['gift_list'];
			}
		}else{
			$info['gift_list'] = $setinfo['gift_list'];
		}
		//gift list isgift
		if($onlyUpdate)
		{
			if(isset($setinfo['gift_list_isgift']))
			{
				$info['gift_list_isgift'] = $setinfo['gift_list_isgift'] && $setinfo['gift_list_isgift'] == 'Y'?'Y':'N';
			}
		}else{
			$info['gift_list_isgift'] = $setinfo['gift_list_isgift'] && $setinfo['gift_list_isgift'] == 'Y'?'Y':'N';
		}
		//benzu_customize_price
		if($onlyUpdate)
		{
			if(isset($setinfo['benzu_customize_price']))
			{
				$info['benzu_customize_price'] = $setinfo['benzu_customize_price'];
			}
		}else{
			$info['benzu_customize_price'] = $setinfo['benzu_customize_price'];
		}
		
		$this->reEstimate();
		
	}
	public function getInfo()
	{
		return $this->info['order'];
	}
	public function setCatalog($catalogName)
	{
		$dtCatalog = new DataTable(constant('DB_TABLE_CATALOG'));
		$catalogData = $dtCatalog->get_row(sprintf("`name`='%s'",Util::sql_safe($catalogName)));
		if(!$catalogData)
			throw new Exception('catalog '.$catalogName.' not exits');
		$catalogHelper = new Catalog_Helper();
		$catalogHelper->init($catalogData['id']);
		$catalogInfo = $catalogHelper->getInfo();
		$orderInfo = $this->getInfo();
		$orderInfo['catalog'] = $catalogInfo['name'];
		$orderInfo['type'] = $catalogInfo['type'];
		$orderInfo['disk_number'] = $catalogInfo['disk_number'];
		$orderInfo['disk_number_add'] = 0;
		$orderInfo['benzu_number'] = $catalogInfo['benzu_number'];
		
		$dtBenzuStyle = new DataTable(constant('DB_TABLE_BENZU_STYLE'));
		$benzuStyleData = $dtBenzuStyle->get($catalogInfo['benzu_style_id']);
		if($benzuStyleData)
			$orderInfo['benzu_style'] = $benzuStyleData['name'];
		
		$orderInfo['person_number'] = $catalogInfo['person_number'];
		$orderInfo['person_number2'] = $catalogInfo['person_number2'];
		$orderInfo['person_number_add'] = 0;
		$orderInfo['dress_number'] = $catalogInfo['dress_number'];
		$orderInfo['dress_number2'] = $catalogInfo['dress_number2'];
		$orderInfo['dress_number_add'] = 0;
		
		$orderInfo['benzu_customize_price'] = $catalogInfo['benzu_customize_price'];
		//zoom list
		$orderInfo['zoom_list'] = array();
		foreach($catalogInfo['photo_size'] as $tmpData)
		{
			//$orderInfo['zoom_list'][] = array('size_id'=>$tmpData->size_id,'size_name'=>$tmpData->size_name,'number'=>$tmpData->number);
			$orderInfo['zoom_list'][] = get_object_vars($tmpData);
		}
		//zoom frame list
		$orderInfo['zoom_frame_list']  = array();
		$tmpCatalogFrameList = get_object_vars($catalogInfo['photo_size_frame']);
		//var_dump($tmpCatalogFrameList);
		foreach($tmpCatalogFrameList as $tmpKey=>$tmpData)
		{
			//$orderInfo['zoom_frame_list'][] = get_object_vars($tmpData);
			foreach($tmpData as $tmpData2)
			{
				$tmpZoomFrameData = get_object_vars($tmpData2);
				$tmpZoomFrameData['size_id'] = $tmpKey;
				$orderInfo['zoom_frame_list'][] = $tmpZoomFrameData;
			}
		}

		//gift list
		$orderInfo['gift_list'] = array();
		foreach($catalogInfo['gift'] as $tmpData)
		{
			$orderInfo['gift_list'][] = get_object_vars($tmpData);
		}
	
		$this->setInfo($orderInfo);
		$this->reEstimate();
	}
	public function setEstimatePrice($priceInfo)
	{
		$orderTagRef = &$this->info['order'];
		$priceTag = &$orderTagRef['price'];
		
		$priceTag['total'] = $priceInfo['total'];
		unset($priceInfo['total']);
		$priceTag['fee_list'] = $priceInfo;
	}
	public function getPriceInfo()
	{
		if(!$this->is_load_xml)
			$this->_loadXmlData();
		return $this->info['order']['price'];
	}
	public function setPayInfo($setinfo,$onlyUpdate = true)
	{
		if(!$this->is_load_xml)
			$this->_loadXmlData();
			
		$orderinfo = &$this->info['order'];
		$info = &$orderinfo['price'];
		//total
		if($onlyUpdate)
		{
			if(isset($setinfo['total']))
			{
				$info['total'] = $setinfo['total'];
			}
		}else{
			$info['total'] = $setinfo['total'];
		}
		if($onlyUpdate)
		{
			if(isset($setinfo['discountPrice']))
			{
				$info['discountPrice'] = $setinfo['discountPrice'];
			}
		}else{
			$info['discountPrice'] = $setinfo['discountPrice'];
		}
		//pre_pay
		if($onlyUpdate)
		{
			if(isset($setinfo['pre_pay']))
			{
				$info['pre_pay'] = $setinfo['pre_pay'];
			}
		}else{
			$info['pre_pay'] = $setinfo['pre_pay'];
		}
		//need_pay
		if($onlyUpdate)
		{
			if(isset($setinfo['need_pay']))
			{
				$info['need_pay'] = $setinfo['need_pay'];
			}
		}else{
			$info['need_pay'] = $setinfo['need_pay'];
		}
	}
	public function setWorkflowInfo($setinfo,$onlyUpdate = true)
	{
		if(!$this->is_load_xml)
			$this->_loadXmlData();
			
		$orderinfo = &$this->info['order'];
		$info = &$orderinfo['workflow'];
		//photo time
		if($onlyUpdate)
		{
			if(isset($setinfo['photo_time']))
			{
				$info['photo_time'] = $setinfo['photo_time'];
			}
		}else{
			$info['photo_time'] = $setinfo['photo_time'];
		}
		//proof time
		if($onlyUpdate)
		{
			if(isset($setinfo['proof_time']))
			{
				$info['proof_time'] = $setinfo['proof_time'];
			}
		}else{
			$info['proof_time'] = $setinfo['proof_time'];
		}
		//pickup time
		if($onlyUpdate)
		{
			if(isset($setinfo['pickup_time']))
			{
				$info['pickup_time'] = $setinfo['pickup_time'];
			}
		}else{
			$info['pickup_time'] = $setinfo['pickup_time'];
		}
		//photo person
		if($onlyUpdate)
		{
			if(isset($setinfo['photo_person_list']))
			{
				$info['photo_person_list'] = $setinfo['photo_person_list'];
			}
		}else{
			$info['photo_person_list'] = $setinfo['photo_person_list'];
		}
		//dress person
		if($onlyUpdate)
		{
			if(isset($setinfo['dress_person_list']))
			{
				$info['dress_person_list'] = $setinfo['dress_person_list'];
			}
		}else{
			$info['dress_person_list'] = $setinfo['dress_person_list'];
		}
		//pickup person
		if($onlyUpdate)
		{
			if(isset($setinfo['pickup_person_list']))
			{
				$info['pickup_person_list'] = $setinfo['pickup_person_list'];
			}
		}else{
			$info['pickup_person_list'] = $setinfo['pickup_person_list'];
		}
		//proof person
		if($onlyUpdate)
		{
			if(isset($setinfo['csr_list']))
			{
				$info['csr_list'] = $setinfo['csr_list'];
			}
		}else{
			$info['csr_list'] = $setinfo['csr_list'];
		}
		
		//add log record
		if(!array_key_exists('workflow_log_list',$orderinfo))
			$orderinfo['workflow_log_list'] = array();
		
		$role = '';
		if(SessionUtil::getVar(constant('SESSION_KEY_USER_IS_ADMIN')) == 'Y')
		{
			$role = '系統管理';
		}else if(SessionUtil::getVar(constant('SESSION_KEY_USER_IS_CSR')) == 'Y')
		{
			$role = '門市部及客服部';
		}else if(SessionUtil::getVar(constant('SESSION_KEY_USER_IS_PHOTO')) == 'Y')
		{
			$role = '攝影部及造型部';
		}else if(SessionUtil::getVar(constant('SESSION_KEY_USER_IS_PRODUCT')) == 'Y')
		{
			$role = '製作部';
		}
		$operator = $role.'-'.SessionUtil::getVar(constant('SESSION_KEY_USER_NAME'));
		$orderinfo['workflow_log_list'][] = array(
			'time'=>date('Y-m-d H:i:s'),
			'operator'=>$operator,
			'msg'=>''
		);
	}
	public function getWorkflowInfo()
	{
		if(!$this->is_load_xml)
			$this->_loadXmlData();
			
		return $this->info['order']['workflow'];
	}
	
	public function injectOrderMainIndex()
	{
		//check order number takecn
		if($this->isOrderNumberTaken($this->info['order']['number']))
		{
			throw new Exception('order number taken');
		}
		//init necessary data
		$refOrderInfo = &$this->info['order'];
		$refOrderInfo['created_by_user'] = SessionUtil::getVar(constant('SESSION_KEY_USER_ID'));
		if ( !isset($refOrderInfo['created'] )) {
			$refOrderInfo['created'] = date('Y-m-d H:i:s');
		}else{
			
		}
		$refOrderInfo['status'] = 'new';
		$refOrderInfo['already_photo'] = 'N';
		$refOrderInfo['already_proof'] = 'N';
		$refOrderInfo['already_pickup'] = 'N';
		//log pre pay
		if($refOrderInfo['price']['pre_pay'] > 0)
		{
			$refOrderInfo['pay_list'][] = array('date'=>date('Y-m-d H:i:s'),'amount'=>$refOrderInfo['price']['pre_pay'],'type'=>'預約','csr'=>SessionUtil::getVar(constant('SESSION_KEY_USER_NAME')));
		}
		$this->persistOrderInfo();
		
		$orderInfo = $this->getInfo();
		
		//copy order define xml
		$order_created_date = strtotime($refOrderInfo['created']);
		if ( $order_created_date === false) {
			$order_created_date  = time();
		}
		$orderDir = constant('SITE_WEB_DISK_DIR').'/'.constant('PATH_ORDER_DIR').'/'.date('Y',$order_created_date ).'/'.date('m',$order_created_date );
		if(!file_exists($orderDir))
		{
			mkdir($orderDir,0755,true);
		}
		$orderXmlPath = $orderDir.'/'.$orderInfo['number'].'.xml';
		copy($this->path,$orderXmlPath);
		
		
		
		//update order manage data
		$dtOrderManger = new DataTable(constant('DB_TABLE_ORDER_MANAGER'));
		if(file_exists($this->path))
		{
			unlink($this->path);
		}
		if(file_exists($this->editing_path))
		{
			unlink($this->editing_path);
		}
		$updateData = array('id'=>$this->getId(),'status'=>Order_Manager::$STATUS_SAVED,'path'=>$orderXmlPath,'editing_path'=>'','is_editing'=>'N');
		$dtOrderManger->save($updateData);
		$this->reload();
		$indexManager = new Order_IndexManager();
		$indexManager->index($this);
	
	}
	/********* private functions begin ****************/
	private function _loadXmlData()
	{
		$enumTags = array(
		'zoom_list',
		'zoom_frame_list',
		'gift_list',
		'gift_detail',
		'photo_person_list',
		'dress_person_list',
		'pickup_person_list',
		'csr_list',
		'pay_list',
		'note_list',
		'workflow_log_list');
		$info = XML::XML_unserialize(file_get_contents($this->getWoringXmlPath()),false,$enumTags);
		
		$refOrderInfo = &$info['order'];
		//correct empty list
		foreach($enumTags as $keyName)
		{
			if($keyName == 'gift_detail')
			{
				if(isset($refOrderInfo['price']['fee_list']['gift_detail'][$keyName]) && count($refOrderInfo['price']['fee_list']['gift_detail'][$keyName]) == 1 && is_string($refOrderInfo['price']['fee_list']['gift_detail'][$keyName][0]) && trim($refOrderInfo['price']['fee_list']['gift_detail'][$keyName][0]) == '')
				{
					$refOrderInfo['price']['fee_list']['gift_detail'][$keyName] = array();
				}
			}else if($keyName == 'photo_person_list'){
				if(isset($refOrderInfo['workflow'][$keyName]) && count($refOrderInfo['workflow'][$keyName]) == 1 && is_string($refOrderInfo['workflow'][$keyName][0]) && trim($refOrderInfo['workflow'][$keyName][0]) == '')
				{
					$refOrderInfo['workflow'][$keyName] = array();
				}
			}else if($keyName == 'dress_person_list'){
				if(isset($refOrderInfo['workflow'][$keyName]) && count($refOrderInfo['workflow'][$keyName]) == 1 && is_string($refOrderInfo['workflow'][$keyName][0]) && trim($refOrderInfo['workflow'][$keyName][0]) == '')
				{
					$refOrderInfo['workflow'][$keyName] = array();
				}
			}else if($keyName == 'pickup_person_list'){
				if(isset($refOrderInfo['workflow'][$keyName]) && count($refOrderInfo['workflow'][$keyName]) == 1 && is_string($refOrderInfo['workflow'][$keyName][0]) && trim($refOrderInfo['workflow'][$keyName][0]) == '')
				{
					$refOrderInfo['workflow'][$keyName] = array();
				}
			}else if($keyName == 'csr_list'){
				if(isset($refOrderInfo['workflow'][$keyName]) && count($refOrderInfo['workflow'][$keyName]) == 1 && is_string($refOrderInfo['workflow'][$keyName][0]) && trim($refOrderInfo['workflow'][$keyName][0]) == '')
				{
					$refOrderInfo['workflow'][$keyName] = array();
				}
			}else{
				if(array_key_exists($keyName, $refOrderInfo) && count($refOrderInfo[$keyName]) == 1 && is_string($refOrderInfo[$keyName][0]) && trim($refOrderInfo[$keyName][0]) == '')
				{
					$refOrderInfo[$keyName] = array();
				}
			}
		}
		$this->info = $info;
		$this->is_load_xml = true;
	}

	public function setAlreadyPhotoFlag($val)
	{
		if(!$this->is_load_xml)
			$this->_loadXmlData();
		$this->info['order']['already_photo'] = $val == 'Y'?'Y':'N';
		
	}
	public function setAlreadyProofFlag($val)
	{
		if(!$this->is_load_xml)
			$this->_loadXmlData();
		$this->info['order']['already_proof'] = $val == 'Y'?'Y':'N';
		
	}
		public function setAlreadyPickupFlag($val)
	{
		if(!$this->is_load_xml)
			$this->_loadXmlData();
		$this->info['order']['already_pickup'] = $val == 'Y'?'Y':'N';
		
	}
	public function addNote($content)
	{
		$this->info['order']['note_list'][] = array('date'=>date('Y-m-d H:i:s'),'content'=>$content);
	}
	public function updateNote($content,$index)
	{
		$this->info['order']['note_list'][$index]['content'] = $content;
	}
	public function addPayOrder($type,$amount,$csr,$sales,$discountInfo, $payMethod)
	{
		$this->info['order']['pay_list'][] = array('date'=>date('Y-m-d H:i:s'),'amount'=>$amount,'type'=>$type,'csr'=>$csr,'sales'=>$sales,'discount'=>$discountInfo,'method'=>$payMethod);
		//update need page
		$this->info['order']['price']['pre_pay'] += $amount;
		$this->info['order']['price']['need_pay']  = $this->info['order']['price']['total'] - $this->info['order']['price']['pre_pay'];
	}
	public function editPayOrder($pIndex,$type,$amount,$csr,$sales,$discountInfo, $payMethod)
	{
		$oldPayInfo = $this->info['order']['pay_list'][$pIndex];
		$this->info['order']['pay_list'][$pIndex] = array('date'=>date('Y-m-d H:i:s'),'amount'=>$amount,'type'=>$type,'csr'=>$csr,'sales'=>$sales,'discount'=>$discountInfo,'method'=>$payMethod);
		//update need page
		$this->info['order']['price']['pre_pay'] += $amount - $oldPayInfo['amount'];
		$this->info['order']['price']['need_pay']  = $this->info['order']['price']['total'] - $this->info['order']['price']['pre_pay'];
	}
	public function deletePayOrder($pIndex)
	{
		$oldPayInfo = $this->info['order']['pay_list'][$pIndex];
		unset($this->info['order']['pay_list'][$pIndex]);
		$this->info['order']['price']['pre_pay'] -=  $oldPayInfo['amount'];
		$this->info['order']['price']['need_pay']  = $this->info['order']['price']['total'] - $this->info['order']['price']['pre_pay'];

	}
	public function setOrderStatus($status)
	{
		$this->info['order']['status'] = $status;
	}
	public function getOrderStatus()
	{
		return $this->info['order']['status'];
	}
	public function isOrderNumberTaken($number)
	{
		$dtMainIndex = new DataTable(constant('DB_TABLE_ORDER_INDEX_MAIN'));
		$condition = sprintf("`order_number`='%s'",$number);
		$row = $dtMainIndex->get_row($condition);
		$isTaken = false;
		if($row)
		{
			if($row['manager_id'] != $this->db_id)
			{
				$isTaken = true;
			}
		}
		return $isTaken;
	}
	private function getWoringXmlPath()
	{
		$path = '';
		if($this->is_editing)
		{
			$path =  $this->editing_path;
		}else{
			$path =  $this->path;
		}
		return $path;
	}
	private function reEstimate()
	{
		try
		{
		$orderEstimatorHelper = new Order_Estimator($this);
		$priceTable = $orderEstimatorHelper->estimate();
		$this->setEstimatePrice($priceTable);
		$this->info['order']['price']['need_pay']  = $priceTable['total'] - $this->info['order']['price']['pre_pay'];
		}catch(Exception $ex)
		{
		}
	}
	private function reIndex()
	{
		$indexManager = new Order_IndexManager();
		$indexManager->index($this);
	}
	/********* private functions end ****************/
	/******* getter and setter begin**************/
	public function getId()
	{
		return $this->db_id;
	}
	public function getStatus()
	{	return $this->status;
	}

	/******* getter and setter end**************/
}
?>